package pers.qianyu.module.core.domain.comment.converter;

import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import org.springframework.stereotype.*;
import pers.qianyu.module.core.comm.*;
import pers.qianyu.module.core.domain.comment.dto.*;
import pers.qianyu.module.core.domain.comment.entity.*;
import pers.qianyu.module.core.domain.comment.vo.*;

import java.util.*;
import java.util.stream.*;

/**
 * @author mizzle_rain
 * @date 2020年10月30日12:37:05
 */
@Component
public class CommentConverter extends BaseConverter {
    /**
     * 将 PO 对象转换为 VO 对象
     *
     * @param commentPO PO对象
     * @return VO 对象
     */
    public CommentVO convertToVO(CommentPO commentPO) {
        CommentVO commentVO = new CommentVO();
        commentVO.setTopicId(commentPO.getTopicId());
        commentVO.setParentId(commentPO.getParentId());
        commentVO.setContent(commentPO.getContent());
        entityToVO(commentPO, commentVO);
        return commentVO;
    }

    /**
     * 将 PO 集合转换为 VO 集合
     *
     * @param commentPOList PO 集合
     * @return VO 集合
     */
    public List<CommentVO> convertToVOList(List<CommentPO> commentPOList) {
        return Optional.ofNullable(commentPOList)
                .map(List::stream)
                .orElseGet(Stream::empty)
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    /**
     * 将 MyBatis-plus 的分页对象转换为自定义分页对象
     *
     * @param commentPOPage MyBatis-plus 分页对象
     * @return 自定义分页对象
     */
    public Pagination<CommentVO> convertToPagination(Page<CommentPO> commentPOPage) {
        Pagination<CommentVO> pagination = new Pagination<>();
        pagination.setCurrent(commentPOPage.getCurrent());
        pagination.setTotal(commentPOPage.getTotal());
        pagination.setSize(commentPOPage.getSize());
        pagination.setRecords(convertToVOList(commentPOPage.getRecords()));
        return pagination;
    }

    /**
     * 将 DTO 对象转换为 PO 对象
     *
     * @param commentDTO DTO 对象
     * @return PO 对象
     */
    public CommentPO convertToPO(CommentDTO commentDTO) {
        CommentPO commentPO = new CommentPO();
        commentPO.setTopicId(commentDTO.getTopicId());
        commentPO.setParentId(commentDTO.getParentId());
        commentPO.setContent(commentDTO.getContent());
        commentPO.setCreatedBy(commentDTO.getCreatedBy());
        commentPO.setUpdatedBy(commentPO.getCreatedBy());
        return commentPO;
    }
}
